/******************************************************
 file : JavaScript
 author : BrighTan
 organization : Digital Media Technology Institute
 function : Fresh data and get more data.
 *******************************************************/
$(".all").height(window.innerHeight);
var contentSlide = $(".content-slide");
freshData(contentSlide[0], 1);
freshData(contentSlide[1], 2);
$(document).ready(function () {

    $(".animsition").animsition({

        inClass: 'zoom-in-sm',
        outClass: 'zoom-out-sm',
        inDuration: 400,
        outDuration: 400,
        linkElement: '.animsition-link',
        // e.g. linkElement   :   'a:not([target="_blank"]):not([href^=#])'
        loading: true,
        loadingParentElement: 'body', //animsition wrapper element
        loadingClass: 'animsition-loading',
        unSupportCss: ['animation-duration',
            '-webkit-animation-duration',
            '-o-animation-duration'
        ],
        //"unSupportCss" option allows you to disable the "animsition" in case the css property in the array is not supported by your browser.
        //The default setting is to disable the "animsition" in a browser that does not support "animation-duration".

        overlay: false,

        overlayClass: 'animsition-overlay-slide',
        overlayParentElement: 'body'
    });
});

function freshData(obj, index) {
    function dealData(data) {
        var content = $(obj);
        content.empty();
        for (var i = 0; i < data.length; i++) {
            var publishedItem = $("<div class=\"publishedItem\">\n" +
                "                        <p></p>\n" +
                "                        <span style=\"width:60%;\">送给：王晓晴\n" +
                "                    </span>\n" +
                "                        <span style=\"float: right;\">2018-4-8 15:20</span>\n" +
                "\n" +
                "                    </div>");
            publishedItem.children().eq(0).text(data[i].songAndSinger);
            publishedItem.children().eq(1).text("送给：" + data[i].orderTo);
            publishedItem.children().eq(2).text(data[i].dateTime);
            publishedItem.appendTo(content);
        }
        var loadingItem = $(" <div class=\"load-container\">\n" +
            "                        <div class=\"loading\">\n" +
            "                    <p>正在加载</p>\n" +
            "                    <span class=\"loadAnimation\">\n" +
            "                        <li class=\"loadItem1 loadItem\"></li>\n" +
            "                        <li class=\"loadItem2 loadItem\"></li>\n" +
            "                        <li class=\"loadItem3 loadItem\"></li>\n" +
            "                    </span>\n" +
            "                </div>");
        content.append(loadingItem)
    }

    if (index === 1)
        $.get("../data/data.json", "freshPublishedData", function (data) {
            dealData(data);
        }, "JSON");
    else if (index === 2)
        $.get("../data/data.json", "freshTokenData", function (data) {
            dealData(data);
        }, "JSON");
}

function moreData(obj, index) {
    console.log("moreData");

    function dealData(data) {
        var content = $(obj);
        for (var i = 0; i < data.length; i++) {
            var publishedItem = $("<div class=\"publishedItem\">\n" +
                "                        <p></p>\n" +
                "                        <span style=\"width:60%;\">送给：王晓晴\n" +
                "                    </span>\n" +
                "                        <span style=\"float: right;\">2018-4-8 15:20</span>\n" +
                "\n" +
                "                    </div>");
            publishedItem.children().eq(0).text(data[i].songAndSinger);
            publishedItem.children().eq(1).text("送给：" + data[i].orderTo);
            publishedItem.children().eq(2).text(data[i].dateTime);
            publishedItem.appendTo(content);
        }
    /*    var loadingItem = $(" <div class=\"load-container\">\n" +
            "                        <div class=\"loading\">\n" +
            "                    <p>正在加载</p>\n" +
            "                    <span class=\"loadAnimation\">\n" +
            "                        <li class=\"loadItem1 loadItem\"></li>\n" +
            "                        <li class=\"loadItem2 loadItem\"></li>\n" +
            "                        <li class=\"loadItem3 loadItem\"></li>\n" +
            "                    </span>\n" +
            "                </div>");
        content.append(loadingItem);*/
        var loadContainer = content.children(".load-container").detach();
        content.append(loadContainer);
        var loading = content.children(".load-container").children();
        loading.children("p").text("加载更多...");
        loading.children("span").toggleClass("load-active", false);
    }

    if (index === 1)
        $.get("../data/data.json", "getMorePublishedData", function (data) {
            console.log("ajax getData successful");
            dealData(data);
        }, "JSON");
    else if (index === 2)
        $.get("../data/data.json", "getMoreTokenData", function (data) {
            dealData(data);
        }, "JSON");
}